<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function antiShake(ms = 1000) {  // 防抖
            let timer = false;
            let num = 0;
            return (...args) => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    console.log(++num)
                }, ms)

            }
        }
        function choke(ms = 1000) { // 节流
            let timer = null;
            let num = 0;
            return () => {
                if (!timer) {
                    timer = setTimeout(() => {
                        console.log(++num)
                        timer = null
                    }, ms)

                }
            }
        }
        // 防抖: 再次触发清零重新计时
        let anti = antiShake()
        // 节流存在则不再创建
        let cho = choke()
    </script>
    <script>
        window.onload = function () {
            document.getElementById('jl').onclick = cho
        }
    </script>
    <button onclick="anti()">防抖：点击关闭或停止定时器</button>
    <button id="jl">节流点击</button>
</body>

</html>